package com.java.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.common.model.Result;
import com.java.common.util.DateUtil;
import com.java.common.util.JwtUtil;
import com.java.manage.dao.CompanyMapper;
import com.java.manage.model.Company;
import com.java.manage.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    @Transactional
    @Override
    public Result save(Company company) {
//        String dateStr = DateUtil.format(new Date(), "yyyyMMdd");

        company.setCreateTime(new Date());

        company.setStatus("1");
        companyMapper.save(company);

        return Result.success();
    }

    @Transactional
    @Override
    public Result modify(Company company) {
        companyMapper.modify(company);

        return Result.success();
    }

    @Transactional
    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            companyMapper.remove(id);
        }

        return Result.success();
    }



    @Override
    public Result list(Company company,HttpServletRequest request) {
        PageHelper.startPage(company.getPageNum(), company.getPageSize());

        String token = request.getHeader("Token");
        Integer storeId =(Integer) JwtUtil.getClaims(token).get("storeId");
        company.setId(storeId);

        List<Company> list = companyMapper.list(company);
        PageInfo<Company> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return Result.success(map);
    }
    @Override
    public Result list(Company company) {
        return null;
    }
    @Override
    public Result get(int id) {
        Company book = companyMapper.get(id);

        return Result.success(book);
    }

    @Override
    public Result listAll() {
        List<Company> companies = companyMapper.listAll();
        return Result.success(companies);
    }
}
